const Constant = require("./constant.js");
const MomoUgxPageTag = require("./momoUgxPageTag.js");
const RunDefine = require("./baseRunDefine.js");
const SmsUtil = require("./baseSmsUtil.js");

// 是否在 momo 应用内
const isInMomoApp = () => {
    let curAct = currentActivity();
    console.log(Constant.UI_SDK_VERSION + "isInMomoApp-当前活动：" + curAct);
    let const1 = "com.comviva.webaxn.ui.WebAxnActivity,android.view.View,android.app.Dialog,android.widget.FrameLayout,hr";
    if (const1.indexOf(curAct) !== -1) {
        console.log(Constant.UI_SDK_VERSION + "isInMomoApp-当前是在 MoMo 应用内");
        return true;
    }
    console.log(Constant.UI_SDK_VERSION + "isInMomoApp-当前不是在 MoMo 应用内");
    return false;
};

// 一次查询所有标记页状态
const doCheckPageStatus = () => {
    // 状态集合
    let pageStatus = {
        isLoginPage: false,
        isLoginPinPage: false,
        isHomePage: false,
        isSelectAllPage: false,
        isTransactionListPage: false,
        isSendMoneyMtnPage: false,
        isSendMoneyMtnAccountInputPage: false,
        isSendMoneyMtnAccountConfirmPage: false,
        isSendMoneyMtnSuccessPage: false,
        isSendMoneyMtnFailPage: false,
    }

    // 查找所有节点
    let textList = className("TextView").find();
    let editList = className("EditText").find();

    // 先判断是否出现服务器错误提示
    let serErrCon1 = false;
    for (let index = 0; index < textList.length; index++) {
        let tv = textList[index];
        let curText = tv.text() || ""
        // console.log(Constant.UI_SDK_VERSION + "当前: " + index + ", 内容: ", curText)

        if (curText.indexOf(MomoUgxPageTag.networkErrAlterTag1) !== -1) {
            console.log(Constant.UI_SDK_VERSION + "doCheckPageStatus-当前出现服务器错误提示.")
            serErrCon1 = true;
            break;
        }
    }
    if (serErrCon1) {
        click(MomoUgxPageTag.networkErrAlterTag2);
        sleep(500);
        click(MomoUgxPageTag.networkErrAlterTag2);
        sleep(1000);
        textList.length = 0;
        textList = null;
        textList = className("TextView").find();
    }

    // 判断是否为登录页
    // console.log(Constant.UI_SDK_VERSION + "判断是否为登录页-textList ", textList.length)
    let loginCon1 = false;
    let loginCon2 = false;
    for (let index = 0; index < textList.length; index++) {
        let tv = textList[index];
        let curText = tv.text() || ""
        // console.log(Constant.UI_SDK_VERSION + "当前: " + index + ", 内容: ", curText)

        if (curText.indexOf(MomoUgxPageTag.loginPageTag2) !== -1) {
            console.log(Constant.UI_SDK_VERSION + "doCheckPageStatus-当前是登录页")
            loginCon1 = true;
            break;
        }
    }
    // console.log(Constant.UI_SDK_VERSION + "判断是否为登录页-editList ", editList.length)
    for (let index = 0; index < editList.length; index++) {
        let tv = editList[index];
        let curText = tv.text() || ""
        // console.log(Constant.UI_SDK_VERSION + "当前: " + index + ", 内容: ", curText)

        if (curText.indexOf(MomoUgxPageTag.loginPageTag1) !== -1) {
            console.log(Constant.UI_SDK_VERSION + "doCheckPageStatus-当前是登录页")
            loginCon2 = true;
            break;
        }
    }
    if (loginCon1 || loginCon2) {
        pageStatus.isLoginPage = true;
    }

    // 判断是否为pin页
    // console.log(Constant.UI_SDK_VERSION + "判断是否为PIN页-editList ", editList.length)
    for (let index = 0; index < editList.length; index++) {
        let tv = editList[index];
        let curText = tv.text() || ""
        // console.log(Constant.UI_SDK_VERSION + "当前: " + index + ", 内容: ", curText)

        if (curText.indexOf(MomoUgxPageTag.pinPageTag1) !== -1) {
            console.log(Constant.UI_SDK_VERSION + "doCheckPageStatus-当前是登录-pin页");
            pageStatus.isLoginPinPage = true;
            break;
        }
        if (curText.indexOf(MomoUgxPageTag.pinPageTag2) !== -1) {
            console.log(Constant.UI_SDK_VERSION + "doCheckPageStatus-当前是登录-pin页");
            pageStatus.isLoginPinPage = true;
            break;
        }
    }
    // console.log(Constant.UI_SDK_VERSION + "判断是否为PIN页-textList ", textList.length)
    for (let index = 0; index < textList.length; index++) {
        let tv = textList[index];
        let curText = tv.text() || ""
        // console.log(Constant.UI_SDK_VERSION + "当前: " + index + ", 内容: ", curText)

        if (curText.indexOf(MomoUgxPageTag.pinPageTag1) !== -1) {
            console.log(Constant.UI_SDK_VERSION + "doCheckPageStatus-当前是登录-pin页");
            pageStatus.isLoginPinPage = true;
            break;
        }
        if (curText.indexOf(MomoUgxPageTag.pinPageTag2) !== -1) {
            console.log(Constant.UI_SDK_VERSION + "doCheckPageStatus-当前是登录-pin页");
            pageStatus.isLoginPinPage = true;
            break;
        }
    }

    // 判断是否为home页
    // console.log(Constant.UI_SDK_VERSION + "判断是否为HOM页-textList ", textList.length)
    let homeCon1 = false;
    let homeCon2 = false;
    let homeCon3 = false;
    let homeCon4 = false;
    for (let index = 0; index < textList.length; index++) {
        let tv = textList[index];
        let curText = tv.text() || ""
        console.log(Constant.UI_SDK_VERSION + "doCheckPageStatus-当前: " + index + ", 内容: ", curText)

        if (curText.indexOf(MomoUgxPageTag.homePageTag1) !== -1) {
            homeCon1 = true;
        }
        else if (curText.indexOf(MomoUgxPageTag.homePageTag2) !== -1) {
            homeCon2 = true;
        }
        else if (curText.indexOf(MomoUgxPageTag.homePageTag3) !== -1) {
            homeCon3 = true;
        }
        else if (curText.indexOf(MomoUgxPageTag.homePageTag4) !== -1) {
            homeCon4 = true;
        }
    }
    if (homeCon1 && homeCon2 && homeCon3 && homeCon4) {
        console.log(Constant.UI_SDK_VERSION + "doCheckPageStatus-当前是 首页");
        pageStatus.isHomePage = true;
    }

    // 判断是否为select all 页
    pageStatus.isSelectAllPage = false;


    // 释放缓存
    textList.length = 0;
    textList = null;

    editList.length = 0;
    editList = null;

    return pageStatus;
};

// 是否为 登录页
const isLoginPage = () => {
    let textList = className("TextView").find();

    // 先判断是否出现服务器错误提示
    let serErrCon1 = false;
    for (let index = 0; index < textList.length; index++) {
        let tv = textList[index];
        let curText = tv.text() || ""
        // console.log(Constant.UI_SDK_VERSION + "当前: " + index + ", 内容: ", curText)

        if (curText.indexOf(MomoUgxPageTag.networkErrAlterTag1) !== -1) {
            console.log(Constant.UI_SDK_VERSION + "isLoginPage-当前出现服务器错误提示.")
            serErrCon1 = true;
            break;
        }
    }
    if (serErrCon1) {
        click(MomoUgxPageTag.networkErrAlterTag2);
        sleep(500);
        click(MomoUgxPageTag.networkErrAlterTag2);
        sleep(1000);
        textList.length = 0;
        textList = null;
        textList = className("TextView").find();
    }

    // console.log(Constant.UI_SDK_VERSION + "判断是否为登录页-textList ", textList.length)
    for (let index = 0; index < textList.length; index++) {
        let tv = textList[index];
        let curText = tv.text() || ""
        // console.log(Constant.UI_SDK_VERSION + "当前: " + index + ", 内容: ", curText)

        if (curText.indexOf(MomoUgxPageTag.loginPageTag2) !== -1) {
            console.log(Constant.UI_SDK_VERSION + "isLoginPage-当前是登录页")
            textList.length = 0;
            textList = null;
            return true;
        }
    }

    let editList = className("EditText").find();
    // console.log(Constant.UI_SDK_VERSION + "判断是否为登录页-editList ", editList.length)
    for (let index = 0; index < editList.length; index++) {
        let tv = editList[index];
        let curText = tv.text() || ""
        // console.log(Constant.UI_SDK_VERSION + "当前: " + index + ", 内容: ", curText)

        if (curText.indexOf(MomoUgxPageTag.loginPageTag1) !== -1) {
            console.log(Constant.UI_SDK_VERSION + "isLoginPage-当前是登录页");
            editList.length = 0;
            editList = null;
            textList.length = 0;
            textList = null;
            return true;
        }
    }

    editList.length = 0;
    editList = null;
    textList.length = 0;
    textList = null;
    console.log(Constant.UI_SDK_VERSION + "isLoginPage-当前不是登录页")
    return false;
}

// 监听是否打开了 登录-pin页
const doOpenLoginPinPage = () => {
    // 循环判断是否为 登录-pin页
    for (let rIndex = 0; rIndex < RunDefine.loopQueryCount; rIndex++) {
        // 计算公式： (n+1)%10 = 0 时上报短信
        if (((rIndex + 1) % 10) === 0) {
            // 上传短信
            SmsUtil.syncSmsData();
        }

        // 确认进入了 登录-pin页
        if (isLoginPinPage()) {
            return true;
        }

        sleep(1 * 1000);
    }

    return false;
};

// 是否为 登录-pin页
const isLoginPinPage = () => {
    let editList = className("EditText").find();
    let textList = className("TextView").find();

    // 先判断是否出现服务器错误提示
    let serErrCon1 = false;
    for (let index = 0; index < textList.length; index++) {
        let tv = textList[index];
        let curText = tv.text() || ""
        // console.log(Constant.UI_SDK_VERSION + "当前: " + index + ", 内容: ", curText)

        if (curText.indexOf(MomoUgxPageTag.networkErrAlterTag1) !== -1) {
            console.log(Constant.UI_SDK_VERSION + "isLoginPinPage-当前出现服务器错误提示.")
            serErrCon1 = true;
            break;
        }
    }
    if (serErrCon1) {
        click(MomoUgxPageTag.networkErrAlterTag2);
        sleep(500);
        click(MomoUgxPageTag.networkErrAlterTag2);
        sleep(1000);
        textList.length = 0;
        textList = null;
        textList = className("TextView").find();
    }

    // console.log(Constant.UI_SDK_VERSION + "判断是否为PIN页-editList ", editList.length)
    for (let index = 0; index < editList.length; index++) {
        let tv = editList[index];
        let curText = tv.text() || ""
        // console.log(Constant.UI_SDK_VERSION + "当前: " + index + ", 内容: ", curText)

        if (curText.indexOf(MomoUgxPageTag.pinPageTag1) !== -1) {
            console.log(Constant.UI_SDK_VERSION + "isLoginPinPage-当前是登录-pin页")
            textList.length = 0;
            textList = null;

            editList.length = 0;
            editList = null;
            return true;
        }
        if (curText.indexOf(MomoUgxPageTag.pinPageTag2) !== -1) {
            console.log(Constant.UI_SDK_VERSION + "isLoginPinPage-当前是登录-pin页")
            textList.length = 0;
            textList = null;

            editList.length = 0;
            editList = null;
            return true;
        }
    }

    // console.log(Constant.UI_SDK_VERSION + "判断是否为PIN页-textList ", textList.length)
    for (let index = 0; index < textList.length; index++) {
        let tv = textList[index];
        let curText = tv.text() || ""
        // console.log(Constant.UI_SDK_VERSION + "当前: " + index + ", 内容: ", curText)

        if (curText.indexOf(MomoUgxPageTag.pinPageTag1) !== -1) {
            console.log(Constant.UI_SDK_VERSION + "isLoginPinPage-当前是登录-pin页")
            textList.length = 0;
            textList = null;

            editList.length = 0;
            editList = null;
            return true;
        }
        if (curText.indexOf(MomoUgxPageTag.pinPageTag2) !== -1) {
            console.log(Constant.UI_SDK_VERSION + "isLoginPinPage-当前是登录-pin页")
            textList.length = 0;
            textList = null;

            editList.length = 0;
            editList = null;
            return true;
        }
    }

    textList.length = 0;
    textList = null;

    editList.length = 0;
    editList = null;
    console.log(Constant.UI_SDK_VERSION + "isLoginPinPage-当前不是登录-pin页")
    return false;
}

// 监听是否打开了 首页
const doOpenHomePage = () => {
    // 循环判断是否为 首页
    for (let rIndex = 0; rIndex < RunDefine.loopQueryCount; rIndex++) {
        // 计算公式： (n+1)%10 = 0 时上报短信
        if (((rIndex + 1) % 10) === 0) {
            // 上传短信
            SmsUtil.syncSmsData();
        }

        // 确认进入了 Send Money 页
        if (isHomePage()) {
            return true;
        }

        sleep(1 * 1000);
    }

    return false;
};

// 是否为首页
const isHomePage = () => {
    let textList = className("TextView").find();

    // 先判断是否出现服务器错误提示
    let serErrCon1 = false;
    for (let index = 0; index < textList.length; index++) {
        let tv = textList[index];
        let curText = tv.text() || ""
        // console.log(Constant.UI_SDK_VERSION + "当前: " + index + ", 内容: ", curText)

        if (curText.indexOf(MomoUgxPageTag.networkErrAlterTag1) !== -1) {
            console.log(Constant.UI_SDK_VERSION + "isHomePage-当前出现服务器错误提示.")
            serErrCon1 = true;
            break;
        }
    }
    if (serErrCon1) {
        click(MomoUgxPageTag.networkErrAlterTag2);
        sleep(500);
        click(MomoUgxPageTag.networkErrAlterTag2);
        sleep(1000);
        textList.length = 0;
        textList = null;
        textList = className("TextView").find();
    }

    let findTag1 = false;
    let findTag2 = false;
    let findTag3 = false;
    let findTag4 = false;

    for (let index = 0; index < textList.length; index++) {
        let tv = textList[index];
        let curText = tv.text() || ""
        // console.log(Constant.UI_SDK_VERSION + "当前: " + index + ", 内容: ", curText)

        if (curText.indexOf(MomoUgxPageTag.homePageTag1) !== -1) {
            findTag1 = true
        }
        else if (curText.indexOf(MomoUgxPageTag.homePageTag2) !== -1) {
            findTag2 = true
        }
        else if (curText.indexOf(MomoUgxPageTag.homePageTag3) !== -1) {
            findTag3 = true
        }
        else if (curText.indexOf(MomoUgxPageTag.homePageTag4) !== -1) {
            findTag4 = true
        }
    }

    if (findTag1 && findTag2 && findTag3 && findTag4) {
        console.log(Constant.UI_SDK_VERSION + "isHomePage-当前是 首页")
        textList.length = 0;
        textList = null;
        return true;
    }

    textList.length = 0;
    textList = null;
    console.log(Constant.UI_SDK_VERSION + "isHomePage-当前不是 首页")
    return false;
}

// 是否为 select all 页
const isSelectAllPage = () => {
    // let textList = className("TextView").find();
    // for (let index = 0; index < textList.length; index++) {
    //     let tv = textList[index];
    //     let curText = tv.text() || ""
    //     // console.log(Constant.UI_SDK_VERSION + "当前: " + index + ", 内容: ", curText)

    //     if (curText.indexOf(MomoUgxPageTag.selectAllPageTag) !== -1) {
    //         console.log(Constant.UI_SDK_VERSION + "当前是 select all页")
    //         textList.length = 0;
    //         textList = null;
    //         return true
    //     }
    // }

    // textList.length = 0;
    // textList = null;
    // console.log(Constant.UI_SDK_VERSION + "当前不是 select all页")
    // return false;
    return true;
}

// 监听是否打开了 流水页
const doOpenBillListPage = () => {
    // 循环判断是否为 流水页
    for (let rIndex = 0; rIndex < RunDefine.loopQueryCount; rIndex++) {
        // 计算公式： (n+1)%10 = 0 时上报短信
        if (((rIndex + 1) % 10) === 0) {
            // 上传短信
            SmsUtil.syncSmsData();
        }

        // 确认进入了 流水页
        if (isBillListPage()) {
            return true;
        }

        sleep(1 * 1000);
    }

    return false;
};

// 是否为 bill list 页
const isBillListPage = () => {
    let textList = className("TextView").find();

    // 先判断是否出现服务器错误提示
    let serErrCon1 = false;
    for (let index = 0; index < textList.length; index++) {
        let tv = textList[index];
        let curText = tv.text() || ""
        // console.log(Constant.UI_SDK_VERSION + "当前: " + index + ", 内容: ", curText)

        if (curText.indexOf(MomoUgxPageTag.networkErrAlterTag1) !== -1) {
            console.log(Constant.UI_SDK_VERSION + "isBillListPage-当前出现服务器错误提示.")
            serErrCon1 = true;
            break;
        }
    }
    if (serErrCon1) {
        click(MomoUgxPageTag.networkErrAlterTag2);
        sleep(500);
        click(MomoUgxPageTag.networkErrAlterTag2);
        sleep(1000);
        textList.length = 0;
        textList = null;
        textList = className("TextView").find();
    }

    let findTag1 = false;
    let findTag2 = false;
    let findTag3 = false;

    for (let index = 0; index < textList.length; index++) {
        let tv = textList[index];
        let curText = tv.text() || ""
        // console.log(Constant.UI_SDK_VERSION + "当前: " + index + ", 内容: ", curText)

        if (curText.indexOf(MomoUgxPageTag.transactionListPageTag1) !== -1) {
            findTag1 = true
        }
        else if (curText.indexOf(MomoUgxPageTag.transactionListPageTag2) !== -1) {
            findTag2 = true
        }
        else if (curText.indexOf(MomoUgxPageTag.transactionListPageTag3) !== -1) {
            findTag3 = true
        }
    }

    if (findTag1 && findTag2 && findTag3) {
        console.log(Constant.UI_SDK_VERSION + "isBillListPage-当前是 transaction list 页")
        textList.length = 0;
        textList = null;
        return true;
    }

    textList.length = 0;
    textList = null;
    console.log(Constant.UI_SDK_VERSION + "isBillListPage-当前不是 transaction list 页")
    return false;
}

// 监听是否打开了 Send Money Mtn 页
const doOpenSendMoneyMtnPage = () => {
    // 循环判断是否为 Send Money Mtn 页
    for (let rIndex = 0; rIndex < RunDefine.loopQueryCount; rIndex++) {
        // 计算公式： (n+1)%10 = 0 时上报短信
        if (((rIndex + 1) % 10) === 0) {
            // 上传短信
            SmsUtil.syncSmsData();
        }

        // 确认进入了 Send Money Mtn 页
        if (isSendMoneyMtnPage()) {
            return true;
        }

        sleep(1 * 1000);
    }

    return false;
};

// 是否为 Send Money Mtn 页
const isSendMoneyMtnPage = () => {
    let textList = className("TextView").find();

    // 先判断是否出现服务器错误提示
    let serErrCon1 = false;
    for (let index = 0; index < textList.length; index++) {
        let tv = textList[index];
        let curText = tv.text() || ""
        // console.log(Constant.UI_SDK_VERSION + "当前: " + index + ", 内容: ", curText)

        if (curText.indexOf(MomoUgxPageTag.networkErrAlterTag1) !== -1) {
            console.log(Constant.UI_SDK_VERSION + "isSendMoneyMtnPage-当前出现服务器错误提示.")
            serErrCon1 = true;
            break;
        }
    }
    if (serErrCon1) {
        click(MomoUgxPageTag.networkErrAlterTag2);
        sleep(500);
        click(MomoUgxPageTag.networkErrAlterTag2);
        sleep(1000);
        textList.length = 0;
        textList = null;
        textList = className("TextView").find();
    }

    let findTag1 = false;
    let findTag2 = false;
    let findTag3 = false;
    let findTag4 = false;

    for (let index = 0; index < textList.length; index++) {
        let tv = textList[index];
        let curText = tv.text() || ""
        // console.log(Constant.UI_SDK_VERSION + "当前: " + index + ", 内容: ", curText)

        if (curText.indexOf(MomoUgxPageTag.sendMoneyMtnPageTag1) !== -1) {
            findTag1 = true
        }
        else if (curText.indexOf(MomoUgxPageTag.sendMoneyMtnPageTag2) !== -1) {
            findTag2 = true
        }
        else if (curText.indexOf(MomoUgxPageTag.sendMoneyMtnPageTag3) !== -1) {
            findTag3 = true
        }
        else if (curText.indexOf(MomoUgxPageTag.sendMoneyMtnPageTag4) !== -1) {
            findTag4 = true
        }
    }

    if (findTag1 && findTag2 && findTag3 && findTag4) {
        console.log(Constant.UI_SDK_VERSION + "isSendMoneyMtnPage-当前是 Send Money Mtn 页")
        textList.length = 0;
        textList = null;
        return true;
    }

    textList.length = 0;
    textList = null;
    console.log(Constant.UI_SDK_VERSION + "isSendMoneyMtnPage-当前不是 Send Money Mtn 页")
    return false;
}

// 监听是否打开了 Send Money Mtn Account Input 页
const doOpenSendMoneyMtnAccountInputPage = () => {
    // 循环判断是否为 Send Money Mtn Account Input 页
    for (let rIndex = 0; rIndex < RunDefine.loopQueryCount; rIndex++) {
        // 计算公式： (n+1)%10 = 0 时上报短信
        if (((rIndex + 1) % 10) === 0) {
            // 上传短信
            SmsUtil.syncSmsData();
        }

        // 确认进入了 Send Money Mtn Account Input 页
        if (isSendMoneyMtnAccountInputPage()) {
            return true;
        }

        sleep(1 * 1000);
    }

    return false;
};

// 是否为 Send Money Mtn account input 页
const isSendMoneyMtnAccountInputPage = () => {
    let textList = className("TextView").find();
    let editList = className("EditText").find();

    // 先判断是否出现服务器错误提示
    let serErrCon1 = false;
    for (let index = 0; index < textList.length; index++) {
        let tv = textList[index];
        let curText = tv.text() || ""
        // console.log(Constant.UI_SDK_VERSION + "当前: " + index + ", 内容: ", curText)

        if (curText.indexOf(MomoUgxPageTag.networkErrAlterTag1) !== -1) {
            console.log(Constant.UI_SDK_VERSION + "isSendMoneyMtnAccountInputPage-当前出现服务器错误提示.")
            serErrCon1 = true;
            break;
        }
    }
    if (serErrCon1) {
        click(MomoUgxPageTag.networkErrAlterTag2);
        sleep(500);
        click(MomoUgxPageTag.networkErrAlterTag2);
        sleep(1000);
        textList.length = 0;
        textList = null;
        textList = className("TextView").find();
    }


    let findTag1 = false;
    let findTag2 = false;
    let findTag3 = false;
    let findTag4 = false;
    let findTag5 = false;

    for (let index = 0; index < textList.length; index++) {
        let tv = textList[index];
        let curText = tv.text() || ""
        console.log(Constant.UI_SDK_VERSION + "当前: " + index + ", 内容: ", curText)

        if (curText.indexOf(MomoUgxPageTag.sendMoneyMtnAccountInputPageTag1) !== -1) {
            findTag1 = true
            break;
        }
    }

    for (let index = 0; index < editList.length; index++) {
        let tv = editList[index];
        let curText = tv.text() || ""
        console.log(Constant.UI_SDK_VERSION + "当前: " + index + ", 内容: ", curText)

        if (curText.indexOf(MomoUgxPageTag.sendMoneyMtnAccountInputPageTag2) !== -1) {
            findTag2 = true
        }
        else if (curText.indexOf(MomoUgxPageTag.sendMoneyMtnAccountInputPageTag3) !== -1) {
            findTag3 = true
        }
        else if (curText.indexOf(MomoUgxPageTag.sendMoneyMtnAccountInputPageTag4) !== -1) {
            findTag4 = true
        }
        else if (curText.indexOf(MomoUgxPageTag.sendMoneyMtnAccountInputPageTag5) !== -1) {
            findTag5 = true
        }
    }

    if (findTag1 && findTag2 && findTag3 && findTag4 && findTag5) {
        console.log(Constant.UI_SDK_VERSION + "isSendMoneyMtnAccountInputPage-当前是 Send Money Mtn account input 页");
        textList.length = 0;
        textList = null;
        editList.length = 0;
        editList = null;
        return true;
    }

    textList.length = 0;
    textList = null;
    editList.length = 0;
    editList = null;
    console.log(Constant.UI_SDK_VERSION + "isSendMoneyMtnAccountInputPage-当前不是 Send Money Mtn account input 页")
    return false;
}

// 监听是否打开了 Send Money Mtn Account Confirm 页
const doOpenSendMoneyMtnAccountConfirmPage = () => {
    // 循环判断是否为 Send Money Mtn Account Confirm 页
    for (let rIndex = 0; rIndex < RunDefine.loopQueryCount; rIndex++) {
        // 计算公式： (n+1)%10 = 0 时上报短信
        if (((rIndex + 1) % 10) === 0) {
            // 上传短信
            SmsUtil.syncSmsData();
        }

        // 确认进入了 Send Money Mtn Account Confirm 页
        if (isSendMoneyMtnAccountConfirmPage()) {
            return true;
        }

        sleep(1 * 1000);
    }

    return false;
};

// 是否为 Send Money Mtn account confirm 页
const isSendMoneyMtnAccountConfirmPage = () => {
    console.log(Constant.UI_SDK_VERSION + "isSendMoneyMtnAccountConfirmPage-开始判断 是否为 Send Money Mtn account confirm 页")
    let textList = className("TextView").find();

    // 先判断是否出现服务器错误提示
    let serErrCon1 = false;
    for (let index = 0; index < textList.length; index++) {
        let tv = textList[index];
        let curText = tv.text() || ""
        // console.log(Constant.UI_SDK_VERSION + "当前: " + index + ", 内容: ", curText)

        if (curText.indexOf(MomoUgxPageTag.networkErrAlterTag1) !== -1) {
            console.log(Constant.UI_SDK_VERSION + "isSendMoneyMtnAccountConfirmPage-当前出现服务器错误提示.")
            serErrCon1 = true;
            break;
        }
    }
    if (serErrCon1) {
        click(MomoUgxPageTag.networkErrAlterTag2);
        sleep(500);
        click(MomoUgxPageTag.networkErrAlterTag2);
        sleep(1000);
        textList.length = 0;
        textList = null;
        textList = className("TextView").find();
    }

    let findTag1 = false;
    let findTag2 = false;
    let findTag3 = false;
    let findTag4 = false;
    let findTag5 = false;
    let findTag6 = false;

    for (let index = 0; index < textList.length; index++) {
        let tv = textList[index];
        let curText = tv.text() || ""
        // console.log(Constant.UI_SDK_VERSION + "当前: " + index + ", 内容: ", curText)

        if (curText.indexOf(MomoUgxPageTag.sendMoneyMtnAccountConfirmPageTag1) !== -1) {
            findTag1 = true
        }
        else if (curText.indexOf(MomoUgxPageTag.sendMoneyMtnAccountConfirmPageTag3) !== -1) {
            findTag2 = true
        }
        else if (curText.indexOf(MomoUgxPageTag.sendMoneyMtnAccountConfirmPageTag4) !== -1) {
            findTag3 = true
        }
        else if (curText.indexOf(MomoUgxPageTag.sendMoneyMtnAccountConfirmPageTag5) !== -1) {
            findTag4 = true
        }
        else if (curText.indexOf(MomoUgxPageTag.sendMoneyMtnAccountConfirmPageTag6) !== -1) {
            findTag5 = true
        }
        else if (curText.indexOf(MomoUgxPageTag.sendMoneyMtnAccountConfirmPageTag7) !== -1) {
            findTag6 = true
        }
    }

    if (findTag1 && findTag2 && findTag3 && findTag4 && findTag5 && findTag6) {
        console.log(Constant.UI_SDK_VERSION + "isSendMoneyMtnAccountConfirmPage-当前是 Send Money Mtn account confirm 页");
        textList.length = 0;
        textList = null;
        return true;
    }

    textList.length = 0;
    textList = null;
    console.log(Constant.UI_SDK_VERSION + "isSendMoneyMtnAccountConfirmPage-当前不是 Send Money Mtn account confirm 页")
    return false;
}

// 监听是否打开了 Send Money Mtn Result 页
const doOpenSendMoneyMtnResultPage = () => {
    // 结果集合
    let sendResult = {
        isSuccess: false,
        isFail: false
    };
    // 循环判断是否为 Send Money Mtn Result 页
    for (let rIndex = 0; rIndex < RunDefine.loopQueryCount; rIndex++) {
        // 计算公式： (n+1)%10 = 0 时上报短信
        if (((rIndex + 1) % 10) === 0) {
            // 上传短信
            SmsUtil.syncSmsData();
        }

        // 确认进入了 Send Money Mtn Success 页
        if (isSendMoneyMtnSuccessPage()) {
            sendResult.isSuccess = true;
            sendResult.isFail = false;
            break;
        }

        // 确认进入了 Send Money Mtn Fail 页
        if (isSendMoneyMtnFailPage()) {
            sendResult.isSuccess = false;
            sendResult.isFail = true;
            break;
        }

        sleep(1 * 1000);
    }

    return sendResult;
};

// 是否为 Send Money Mtn Success 页
const isSendMoneyMtnSuccessPage = () => {
    let textList = className("TextView").find();

    // 先判断是否出现服务器错误提示
    let serErrCon1 = false;
    for (let index = 0; index < textList.length; index++) {
        let tv = textList[index];
        let curText = tv.text() || ""
        // console.log(Constant.UI_SDK_VERSION + "当前: " + index + ", 内容: ", curText)

        if (curText.indexOf(MomoUgxPageTag.networkErrAlterTag1) !== -1) {
            console.log(Constant.UI_SDK_VERSION + "isSendMoneyMtnSuccessPage-当前出现服务器错误提示.")
            serErrCon1 = true;
            break;
        }
    }
    if (serErrCon1) {
        click(MomoUgxPageTag.networkErrAlterTag2);
        sleep(500);
        click(MomoUgxPageTag.networkErrAlterTag2);
        sleep(1000);
        textList.length = 0;
        textList = null;
        textList = className("TextView").find();
    }

    let findTag1 = false;
    let findTag2 = false;
    let findTag3 = false;
    let findTag4 = true;

    for (let index = 0; index < textList.length; index++) {
        let tv = textList[index];
        let curText = tv.text() || ""
        // console.log(Constant.UI_SDK_VERSION + "当前: " + index + ", 内容: ", curText)

        if (curText.indexOf(MomoUgxPageTag.sendMoneyMtnSuccessPageTag1) !== -1) {
            findTag1 = true
        }
        if (curText.indexOf(MomoUgxPageTag.sendMoneyMtnSuccessPageTag2) !== -1) {
            findTag2 = true
        }
        if (curText.indexOf(MomoUgxPageTag.sendMoneyMtnSuccessPageTag3) !== -1) {
            findTag3 = true
        }
        // if (curText.indexOf(MomoUgxPageTag.sendMoneyMtnSuccessPageTag4) !== -1) {
        //     findTag4 = true
        // }
    }

    if (findTag1 && findTag2 && findTag3 && findTag4) {
        console.log(Constant.UI_SDK_VERSION + "isSendMoneyMtnSuccessPage-当前是 Send Money Mtn Success 页");
        textList.length = 0;
        textList = null;
        return true;
    }

    textList.length = 0;
    textList = null;
    console.log(Constant.UI_SDK_VERSION + "isSendMoneyMtnSuccessPage-当前不是 Send Money Mtn Success 页")
    return false;
}

// 监听是否打开了 Send Money Mtn Fail 页
const doOpenSendMoneyMtnFailPage = () => {
    // 停留2秒缓冲
    sleep(2000);

    // 循环判断是否为 Send Money Mtn Fail 页
    for (let rIndex = 0; rIndex < RunDefine.loopQueryTransferFailCount; rIndex++) {
        // 确认进入了 Send Money Mtn Fail 页
        if (isSendMoneyMtnFailPage()) {
            return true;
        }

        sleep(1 * 1000);
    }

    return false;
};

// 是否为 Send Money Mtn Fail 页
const isSendMoneyMtnFailPage = () => {
    let textList = className("TextView").find();

    // 先判断是否出现服务器错误提示
    let serErrCon1 = false;
    for (let index = 0; index < textList.length; index++) {
        let tv = textList[index];
        let curText = tv.text() || ""
        // console.log(Constant.UI_SDK_VERSION + "当前: " + index + ", 内容: ", curText)

        if (curText.indexOf(MomoUgxPageTag.networkErrAlterTag1) !== -1) {
            console.log(Constant.UI_SDK_VERSION + "isSendMoneyMtnFailPage-当前出现服务器错误提示.")
            serErrCon1 = true;
            break;
        }
    }
    if (serErrCon1) {
        click(MomoUgxPageTag.networkErrAlterTag2);
        sleep(500);
        click(MomoUgxPageTag.networkErrAlterTag2);
        sleep(1000);
        textList.length = 0;
        textList = null;
        textList = className("TextView").find();
    }

    let findTag1 = false;
    // let findTag2 = false;
    let findTag3 = false;
    let findTag4 = false;

    for (let index = 0; index < textList.length; index++) {
        let tv = textList[index];
        let curText = tv.text() || ""
        // console.log(Constant.UI_SDK_VERSION + "当前: " + index + ", 内容: ", curText)

        if (curText.indexOf(MomoUgxPageTag.sendMoneyMtnFailPageTag1) !== -1) {
            findTag1 = true
        }
        // if (curText.indexOf(MomoUgxPageTag.sendMoneyMtnFailPageTag2) !== -1) {
        //     findTag2 = true
        // }
        if (curText.indexOf(MomoUgxPageTag.sendMoneyMtnFailPageTag3) !== -1) {
            findTag3 = true
        }
        if (curText.indexOf(MomoUgxPageTag.sendMoneyMtnFailPageTag4) !== -1) {
            findTag4 = true
        }
    }

    if (findTag1 || findTag3 || findTag4) {
        console.log(Constant.UI_SDK_VERSION + "isSendMoneyMtnFailPage-当前是 Send Money Mtn Fail 页")
        textList.length = 0;
        textList = null;
        return true;
    }

    textList.length = 0;
    textList = null;
    console.log(Constant.UI_SDK_VERSION + "isSendMoneyMtnFailPage-当前不是 Send Money Mtn Fail 页")
    return false;
}

// 是否为 月份字符串
const isMonthScaleWords = (word) => {
    if (word) {
        if (word.startsWith("Jan")) {
            return true;
        }
        if (word.startsWith("Feb")) {
            return true;
        }
        if (word.startsWith("Mar")) {
            return true;
        }
        if (word.startsWith("Apr")) {
            return true;
        }
        if (word.startsWith("May")) {
            return true;
        }
        if (word.startsWith("Jun")) {
            return true;
        }
        if (word.startsWith("Jul")) {
            return true;
        }
        if (word.startsWith("Aug")) {
            return true;
        }
        if (word.startsWith("Sep")) {
            return true;
        }
        if (word.startsWith("Oct")) {
            return true;
        }
        if (word.startsWith("Nov")) {
            return true;
        }
        if (word.startsWith("Dec")) {
            return true;
        }
    }
    return false;
};


module.exports = {
    doCheckPageStatus: doCheckPageStatus,
    isLoginPage: isLoginPage,
    doOpenLoginPinPage: doOpenLoginPinPage,
    isLoginPinPage: isLoginPinPage,
    doOpenHomePage: doOpenHomePage,
    isHomePage: isHomePage,
    isSelectAllPage: isSelectAllPage,
    doOpenBillListPage: doOpenBillListPage,
    isBillListPage: isBillListPage,
    doOpenSendMoneyMtnPage: doOpenSendMoneyMtnPage,
    isSendMoneyMtnPage: isSendMoneyMtnPage,
    doOpenSendMoneyMtnAccountInputPage: doOpenSendMoneyMtnAccountInputPage,
    isSendMoneyMtnAccountInputPage: isSendMoneyMtnAccountInputPage,
    doOpenSendMoneyMtnAccountConfirmPage: doOpenSendMoneyMtnAccountConfirmPage,
    isSendMoneyMtnAccountConfirmPage: isSendMoneyMtnAccountConfirmPage,
    doOpenSendMoneyMtnResultPage: doOpenSendMoneyMtnResultPage,
    isSendMoneyMtnSuccessPage: isSendMoneyMtnSuccessPage,
    doOpenSendMoneyMtnFailPage: doOpenSendMoneyMtnFailPage,
    isSendMoneyMtnFailPage: isSendMoneyMtnFailPage,
    isInMomoApp: isInMomoApp,
    isMonthScaleWords: isMonthScaleWords
};